package com.jwds.digital.bios.business.service.service.dispatch.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchReceivingMaterialsReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.assembler.DispatchReceivingMaterialsAssembler;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.BasicStockBinReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.FlowBasicReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.model.DeviceStatusDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.model.FlowPlcDTO;
import com.jwds.digital.bios.business.core.infrastructure.entity.dispatch.DispatchReceivingMaterials;
import com.jwds.digital.bios.business.core.infrastructure.export.DispatchReceivingMaterialsExport;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltRouteTargetHisMapper;
import com.jwds.digital.bios.business.core.infrastructure.mapper.dispatch.DispatchReceivingMaterialsMapper;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltRouteTargetHisRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.dispatch.DispatchReceivingMaterialsRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.BasicStockBinRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.FlowBasicRespVO;
import com.jwds.digital.bios.business.core.utils.FeignResultUtil;
import com.jwds.digital.bios.business.rpc.feign.BasicStockBinFeign;
import com.jwds.digital.bios.business.rpc.feign.FlowBasicFeign;
import com.jwds.digital.bios.business.rpc.feign.RealTimeDataFeign;
import com.jwds.digital.bios.business.service.service.dispatch.IDispatchReceivingMaterialsService;
import com.jwds.tdc.framework.application.lib.BaseResponse;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 料场受料调度实现类
 *
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class DispatchReceivingMaterialsServiceImpl extends ServiceImpl<DispatchReceivingMaterialsMapper, DispatchReceivingMaterials> implements IDispatchReceivingMaterialsService {

    @Autowired
    private DispatchReceivingMaterialsMapper baseMapper;
    @Autowired
    private BasicStockBinFeign basicStockBinFeign;
    @Autowired
    private FlowBasicFeign flowBasicFeign;

    @Autowired
    private BeltRouteTargetHisMapper beltRouteTargetHisMapper;

    @Autowired
    HttpServletResponse response;

    @Override
    public DispatchReceivingMaterialsRespVO queryById(Long id) {
        if(id == null) {
            return null;
        }
        DispatchReceivingMaterials entity = baseMapper.selectById(id);
        return DispatchReceivingMaterialsAssembler.entity2dto(entity);
    }

    @Override
    public PageResponse<DispatchReceivingMaterialsRespVO> page(PageRequest<DispatchReceivingMaterialsReqDTO> request) {
        DispatchReceivingMaterialsReqDTO condition = request.getQueryCondition();
        Page<DispatchReceivingMaterialsRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
        IPage<DispatchReceivingMaterialsRespVO> pageData = baseMapper.pageQuery(page, condition);
        return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
    }

    @Override
    public List<DispatchReceivingMaterialsRespVO> findAll() {
        return baseMapper.queryList();
    }

    @Override
    public void add(DispatchReceivingMaterialsReqDTO reqDto) {
        DispatchReceivingMaterials entity = DispatchReceivingMaterialsAssembler.dto2entity(reqDto);
        baseMapper.insert(entity);
    }


    @Override
    public void update(DispatchReceivingMaterialsReqDTO reqDto) {
        DispatchReceivingMaterials oldEntity = baseMapper.selectById(reqDto.getId());
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        DispatchReceivingMaterials newEntity = DispatchReceivingMaterialsAssembler.dto2entity(reqDto, oldEntity);
        baseMapper.updateById(newEntity);
    }

    @Override
    public void delete(Long id) {
        DispatchReceivingMaterials oldEntity = baseMapper.selectById(id);
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        baseMapper.deleteById(id);
    }


    @Override
    public List<String> DropDownBox(String destPointion) {
        if (destPointion==null){
            throw new RecordNotFoundException();
        }
        ArrayList<String> strings = new ArrayList<>();
        LambdaQueryWrapper<DispatchReceivingMaterials> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DispatchReceivingMaterials::getDestPointion,destPointion);
        List<DispatchReceivingMaterials> list = baseMapper.selectList(queryWrapper);
        for (DispatchReceivingMaterials datum : list) {
            strings.add(datum.getDestPointion());
        }
        return strings;
    }

    @Override
    public void export()  {
        // 设置文件路径
        String fileName = "D:\\dispatchReceivingMaterials.xlsx";
        List<DispatchReceivingMaterialsRespVO> list = baseMapper.queryList();
        List<DispatchReceivingMaterialsExport> lists = new ArrayList<>();
        for (DispatchReceivingMaterialsRespVO dispatchReceivingMaterialsRespVO : list) {
            DispatchReceivingMaterialsExport dispatchReceivingMaterialsExport = new DispatchReceivingMaterialsExport();
            dispatchReceivingMaterialsExport.setId(dispatchReceivingMaterialsRespVO.getId()+"");
            dispatchReceivingMaterialsExport.setWarehouseCode(dispatchReceivingMaterialsRespVO.getWarehouseCode()+"");
            dispatchReceivingMaterialsExport.setWarehouseName(dispatchReceivingMaterialsRespVO.getWarehouseName());
            dispatchReceivingMaterialsExport.setOriginPointion(dispatchReceivingMaterialsRespVO.getOriginPointion());
            dispatchReceivingMaterialsExport.setDestPointion(dispatchReceivingMaterialsRespVO.getDestPointion());
            dispatchReceivingMaterialsExport.setDestDevice(dispatchReceivingMaterialsRespVO.getDestDevice());
            dispatchReceivingMaterialsExport.setSendTime(dispatchReceivingMaterialsRespVO.getSendTime()+"");
            dispatchReceivingMaterialsExport.setTaskNo(dispatchReceivingMaterialsRespVO.getTaskNo());
            dispatchReceivingMaterialsExport.setWorkType(dispatchReceivingMaterialsRespVO.getWorkType()+"");
            dispatchReceivingMaterialsExport.setHeapCode(dispatchReceivingMaterialsRespVO.getHeapCode());
            dispatchReceivingMaterialsExport.setPlanWeight(dispatchReceivingMaterialsRespVO.getPlanWeight()+"");
            dispatchReceivingMaterialsExport.setSupplier(dispatchReceivingMaterialsRespVO.getSupplier());
            dispatchReceivingMaterialsExport.setLineUpVehicles(dispatchReceivingMaterialsRespVO.getLineUpVehicles()+"");
            dispatchReceivingMaterialsExport.setVehicles(dispatchReceivingMaterialsRespVO.getVehicles()+"");
            dispatchReceivingMaterialsExport.setMaterialCode(dispatchReceivingMaterialsRespVO.getMaterialCode()+"");
            dispatchReceivingMaterialsExport.setMaterialName(dispatchReceivingMaterialsRespVO.getMaterialName());
            lists.add(dispatchReceivingMaterialsExport);
        }

        EasyExcel.write(fileName,DispatchReceivingMaterialsExport.class).sheet("受料调度信息").doWrite(lists);
    }


    @Autowired
    RealTimeDataFeign realTimeDataFeign;
    @Override
    public List<FlowBasicRespVO> Process(Long id) {
        if (id==null){
            throw new RecordNotFoundException();
        }
        DispatchReceivingMaterials materials = baseMapper.selectById(id);
        if (materials==null){
            throw new RecordNotFoundException();
        }
        FlowBasicReqDTO flowBasicReqDTO = new FlowBasicReqDTO();
        flowBasicReqDTO.setStartDeviceCode(materials.getOriginDevice());
        flowBasicReqDTO.setEndDeviceCode(materials.getDestDevice());
        List<FlowBasicRespVO> data = FeignResultUtil.getDataFromBaseResponse(flowBasicFeign.all(flowBasicReqDTO));
        if (data==null||data.size()==0){
            throw new RecordNotFoundException();
        }
        for (FlowBasicRespVO flowBasicRespVO : data) {
            String[] split = flowBasicRespVO.getFlowName().split(StringPool.DASH);
            List<String> list = Arrays.asList(split);
            Map<String, FlowPlcDTO> data1 = realTimeDataFeign.queryFlowPlcDataMap(list).getData();
            Iterator<Map.Entry<String, FlowPlcDTO>> iterator = data1.entrySet().iterator();
            while (iterator.hasNext()) {
                FlowPlcDTO value = iterator.next().getValue();
                if (value.getOcc()){
                    flowBasicRespVO.setFlowStatus(1);
                    break;
                }else {
                    flowBasicRespVO.setFlowStatus(0);
                }
            }
        }
        return data;
    }
}
